﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input.Touch;

namespace MyMusic
{
    public partial class GamePage : PhoneApplicationPage
    {
        ContentManager contentManager;
        GameTimer timer;
        SpriteBatch spriteBatch;

        private float speed = 6;//滚动速度
        private Texture2D background;//背景

        List<Cycles> list = new List<Cycles>();

        List<Cycle> listNote = new List<Cycle>();

        SoundEffect Dao;
        SoundEffect Ruai;
        SoundEffect Mi;
        SoundEffect Fa;
        SoundEffect Shao;
        SoundEffect La;
        SoundEffect Xi;

        DateTime StartTime;

        public GamePage()
        {
            InitializeComponent();

            // Get the content manager from the application
            contentManager = (Application.Current as App).Content;

            // Create a timer for this page
            timer = new GameTimer();
            timer.UpdateInterval = TimeSpan.FromTicks(333333);
            timer.Update += OnUpdate;
            timer.Draw += OnDraw;
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            // Set the sharing mode of the graphics device to turn on XNA rendering
            SharedGraphicsDeviceManager.Current.GraphicsDevice.SetSharingMode(true);

            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(SharedGraphicsDeviceManager.Current.GraphicsDevice);

            // TODO: use this.content to load your game content here

            // Start the timer
            timer.Start();
            TouchPanel.EnabledGestures = GestureType.Tap;
            StartTime = DateTime.Now;


            //实例化七音符
            Dao = contentManager.Load<SoundEffect>("1");
            Ruai = contentManager.Load<SoundEffect>("2");
            Mi = contentManager.Load<SoundEffect>("3");
            Fa = contentManager.Load<SoundEffect>("4");
            Shao = contentManager.Load<SoundEffect>("5");
            La = contentManager.Load<SoundEffect>("6");
            Xi = contentManager.Load<SoundEffect>("7");

            // 创建一个新的SpriteBatch,可以用来画贴图。
            spriteBatch = new SpriteBatch(SharedGraphicsDeviceManager.Current.GraphicsDevice);
            background = contentManager.Load<Texture2D>("background");//加载游戏背景

            LoadAllResource();

            base.OnNavigatedTo(e);
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            // Stop the timer
            timer.Stop();

            // Set the sharing mode of the graphics device to turn off XNA rendering
            SharedGraphicsDeviceManager.Current.GraphicsDevice.SetSharingMode(false);

            base.OnNavigatedFrom(e);
        }

        /// <summary>
        /// Allows the page to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        private void OnUpdate(object sender, GameTimerEventArgs e)
        {
            // TODO: Add your update logic here
            Update();

            //判断点击
            IsClickSuccess();

            UPLoadResource((DateTime.Now - StartTime).TotalMilliseconds);
        }

        /// <summary>
        /// Allows the page to draw itself.
        /// </summary>
        private void OnDraw(object sender, GameTimerEventArgs e)
        {
            SharedGraphicsDeviceManager.Current.GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: Add your drawing code here

            spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.NonPremultiplied);
            Draw();
            //绘制背景
            spriteBatch.Draw(background, Vector2.Zero, Color.White);
            spriteBatch.End();
        }
    }
}